<?php  
	namespace Home\Controller;
	use Think\Controller;
	//教室管理
	//@author  程传民
	class ClassRoomController extends AllowController{
		
		public function index(){
			$mod = D("class_room");
			$tot = $mod->count();
			$list = $mod->select();

			$nums = 5;
			$page = new \Think\Page($tot,$nums);

	    	$list=  $mod->limit($page->firstRow,$page->listRows)->select();
	    	//把时间戳转换成日期时分秒
	    	foreach($list as $key => $value){
	    		if($list[$key]['addtime']==""){
	    			$list[$key]['addtime']="";
	    		}else{
	    			$list[$key]['addtime'] = date("Y-m-d H:i:s",$value['addtime']);
	    		}
	    	}
	    	// 接受当前页,默认打开为1
	    	if (empty($_GET['p'])) {
	    		$p = "1";
	    	}else{
	    		$p = $_GET['p'];
	    	}
	    	// 分配总条数
			$this->assign("total",$tot);
			// 分配进一取整后的总页数
			$this->assign("page",ceil($tot/$nums));
			// 分配当前页的页数
			$this->assign("curr",$p);
			$this->assign("list",$list);
			$this->display("ClassRoom/classroom");
		}

		public function edit(){
			$mod = D("class_room");
			$arr = array("id"=>I("get.id"));
			$list = $mod->where($arr)->find();
			$this->assign("list",$list);
			$this->display("ClassRoom/update");
		}

		public function update(){
			$arr = array("id"=>I("post.ids"));
			$data['name'] = I("post.roomname");
			$mod = D("class_room");
			$info = $mod->where($arr)->save($data);
			if($info){
				echo "修改成功";
			}else{
				echo "修改失败";
			}
		}

		public function delete(){
			$arr = array("class_room"=>I("post.id"));
			$class = D("class");

			//首先判断此教室是否正在使用

			$info = $class->where($arr)->find();
			if($info['status']<=1){
				echo "失败";
			}else{
				$mod = D("class_room");
				$arrs = array("id"=>I("post.id"));
				$info1 = $mod->where($arrs)->delete();
				echo "成功";
			}
		}

		public function add(){
			$this->display("ClassRoom/add");
		}

		public function insert(){
			$mod = D("class_room");
			$data['name'] = I("post.roomname");
			$data['addtime'] = date(time());
			$info = $mod->add($data);
			if($info){
				echo "添加成功";
			}else{
				echo "失败";
			}
		}

	}

?>